// pages/manage/manage.js
var util = require('../../utils/util')
var {request} =require('../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },
  toUser: function () {
    wx.navigateTo({
      url: './user/user',
    })
  },
  toOrder: function () {
    wx.navigateTo({
      url: './order/order',
    })
  },
  toWithdrawal: function () {
    wx.navigateTo({
      url: './withdrawal/withdrawal',
    })
  },
  toSale: function () {
    wx.navigateTo({
      url: './sale/sale',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  payfee: function () {
    var that = this;
    var stamp = util.getStamp(util.formatTime(new Date()));
    var str = util.getRandomCode(12)
    let No = "LB" + stamp + util.getRandomCode()
    wx.showLoading({
      title: '支付中',
    })
    wx.cloud.callFunction({
      name: 'payment',
      data: {
        str: str,
        body: "蜂鸟创客（深圳）技术有限公司-延保支付",
        No: No,
        totalFee: 1
      },
      success(res) {
        console.log(res)
        that.pay(res.result.payment)
        wx.hideLoading()
      },
      fail(error) {
        wx.hideLoading()
        console.log(error)
      },
    })

  },
  pay(payData) {
    var py = this;
    wx.requestPayment({
      nonceStr: payData.nonceStr,
      package: payData.package,
      paySign: payData.paySign,
      timeStamp: payData.timeStamp,
      signType: 'MD5',
      success(res) {
        console.log(res)
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 2000
        })
      },
      fail(res) {
        console.log(res)
        wx.showToast({
          title: '支付未成功',
          duration: 1000,
          image: "none",
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    request('/withdrawal/custom',{
      query:'select count(openid) as count from warranty_user'
    },'POST').then(res=>{
      console.log(res)
      that.setData({
        countUser:res.data[0].count
      })
    })
    request('/withdrawal/custom',{
      query:'select count(*) as total from warranty_withdrawal where status="waiting" '
    },'POST').then(res=>{
      that.setData({
        countWithdrawal:res.data[0].total
      })
    })
    request('/withdrawal/custom',{
      query:'select count(openid) as total from warranty_sale '
    },'POST').then(res=>{
      that.setData({
        countSale:res.data[0].total
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})